package com.sec.android.app.myfiles.fragment.trash;

import android.os.Bundle;
import android.view.ActionMode;
import android.view.View;
import android.view.ViewStub;
import android.widget.Toast;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.facade.MyFilesFacade;
import com.sec.android.app.myfiles.feature.CloudMgr;
import com.sec.android.app.myfiles.fragment.filelist.FileListFragment;
import com.sec.android.app.myfiles.fragment.filelist.PathIndicator;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.util.UiUtils;

/**
 * Created by gw8951.kim on 2016-11-10.
 */

public class TrashFileListFragment extends FileListFragment implements CloudMgr.CloudStateListener {
    private CloudMgr mCloudMgr;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (mNavigationInfo != null) {
            mCloudMgr = CloudMgr.getInstance(mContext);
            mCloudMgr.addListener(this);
        }
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (mActivity != null) {
            mActivity.invalidateOptionsMenu();
        }
        setTrashTitle();
        setTrashExpireNotify(view);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mActivity != null) {
            mActivity.invalidateOptionsMenu();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mCloudMgr.removeListener(this);
    }

    @Override
    public void setPathIndicator(PathIndicator pathIndicator) {
        if (pathIndicator != null) {
            pathIndicator.setFragment(this);
            pathIndicator.setVisibility(View.GONE);
        }
    }

    @Override
    protected boolean _onItemClick(FileRecord record) {
        if (!isSelectActionMode()) {
            if (record.isDirectory()) {
                Toast.makeText(mContext, R.string.open_folder_warning_in_trash, Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(mContext, R.string.open_file_warning_in_trash, Toast.LENGTH_LONG).show();
            }
        }
        return false;
    }

    @Override
    protected boolean _onItemLongClick(View view, int position) {
        if (mAdapter == null) {
            return false;
        }

        FileRecord record = mAdapter.getFileRecord(position);
        if (UiUtils.isSyncing(record)) {
            return false;
        }

        return super._onItemLongClick(view, position);
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {
        super.onDestroyActionMode(mode);
        setTrashTitle();
    }

    @Override
    public void onLoadFinished() {
        super.onLoadFinished();
        setTrashTitle();
    }

    @Override
    public void onAccountChanged(FileRecord.CloudType cloudType) {
        if (mNavigationInfo != null) {
            if (mCloudMgr != null && !mCloudMgr.isSignedIn(FileRecord.CloudType.SamsungDrive)) {
                MyFilesFacade.goHome(mProcessId, mNavigationInfo, mContext);
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (getLoadingStatus()) {
            setLoadingView();
        }
    }

    public void setTrashTitle() {
        if (mActivity != null && mActivity.getActionBar() != null) {
            int count = (mAdapter != null) ? mAdapter.getItemCount() : 0;
            if (count > 0) {
                mActivity.getActionBar().setTitle(mContext.getString(R.string.trash) + "(" + count + ")");
            } else {
                mActivity.getActionBar().setTitle(mContext.getString(R.string.trash));
            }
        }
    }

    public void setTrashExpireNotify(View parentView) {
        ViewStub stub = null;
        if (getExpireNotifyStubId() >= 0) {
            stub = (ViewStub) parentView.findViewById(getExpireNotifyStubId());
        }

        if (stub != null) {
            stub.inflate();
        }
    }

    private int getExpireNotifyStubId() {
        return R.id.expire_notify_stub;
    }
}
